<?php
if(!isset($_SESSION['user'])){
die();
}

require("public/exts/xcrud/xcrud.php");
		$xcrud = Xcrud::get_instance();
		
		
		
        $xcrud->table('blog_video');
		if($_SESSION['user']['level']!=1){
		$xcrud->where('upload_user', $_SESSION['user']['username']);
		}
		
		if($_SESSION['user']['level']==1){
		$xcrud->columns('id,name,image,url,category,subcategory,tag,upload_time,upload_user,pick,status');
		}else{
		$xcrud->columns('id,name,image,url,category,subcategory,tag,upload_time,status');
		}
		//Dependent dropdown
		//Dependence dropdown
		$xcrud->relation('category','blog','id','name','blog.type = "video" and blog.parent is NULL');
		$xcrud->relation('subcategory','blog','id','name','','','',' ','','parent','category');
		
		
		if($_SESSION['user']['level']==1){
		$xcrud->create_action('pass_blog_video', 'blog_pass_video'); //duyet truyen 
		$xcrud->column_pattern('status','<a class="xcrud-button xcrud-action" href="#"  data-task="action" data-action="pass_blog_video" data-primary="{id}"><i class="glyphicon glyphicon-ok state10-{status}"></i><i class="glyphicon glyphicon-remove state10-{status}"></i></a>'); 
		
		$xcrud->create_action('pick_blog_video', 'blog_pick_video'); //duyet truyen 
		$xcrud->column_pattern('pick','<a class="xcrud-button xcrud-action" href="#"  data-task="action" data-action="pick_blog_video" data-primary="{id}"><i class="glyphicon glyphicon-ok state10-{pick}"></i><i class="glyphicon glyphicon-remove state10-{pick}"></i></a>'); 
		}
		
		$xcrud->fields(array('name','description','url','category','subcategory','tag'));
		
		$xcrud->change_type("description",'textarea');
		$xcrud->change_type('image', 'remote_image');
		
	 //Apply select2 example
		$xcrud->set_attr('tag',array('id'=>'tag_select2'));
	//End select2
		
		$xcrud->label(array('name' => 'Tên Video','description'=>'Mô Tả',"url"=>"Link Youtube","category"=>"Danh Mục"));
		
	
		
		$xcrud->pass_var('upload_user',$_SESSION['user']['username'],'create');
		$xcrud->pass_var('upload_time',date('Y-m-d'),'create');
		
		$xcrud->no_quotes("alias");
		
		$xcrud->before_insert("blog_video_before_insert","blog.php");
		 $xcrud->after_insert("blog_video_after_insert","blog.php");
		$xcrud->before_update("blog_video_before_update","blog.php");
	
		$xcrud->unset_csv();
		$xcrud->unset_print();
		$xcrud->unset_title();
		
		
		$xcrud->set_lang('save_return','Lưu Lại & Trở Về');
		$xcrud->set_lang('return','Trở Về');
		$xcrud->set_lang('add','Thêm Video Mới');
		$xcrud->set_lang('save_edit','Lưu Lại & Tiếp Tục Cập Nhật Thông Tin');
		$xcrud->set_lang('save_new','Lưu Lại & Và Thêm Video Khác');
	?>
      	 <ul class="breadcrumb">
          	<li><a href="<?php echo HOME; ?>"><i class="fa fa-home"></i> Home</a></li>
             <li><a href="<?php echo HOME;?>index.php?module=manage&act=home">Quản Lý</a></li>
              <li><a href="<?php echo url();?>">Video</a></li>
          </ul>
    
    <?php
		echo $xcrud->render();


?>
       
       
          
         <script type="text/javascript">
jQuery(document).on("xcrudbeforerequest", function(event, container) {
    if (container) {
        jQuery(container).find("#tag_select2").select2("destroy");
    } else {
        jQuery(".xcrud").find("#tag_select2").select2("destroy");
    }
});
jQuery(document).on("ready xcrudafterrequest", function(event, container) {
    if (container) {
		$("#tag_select2").removeClass("form-control");

        jQuery(container).find("#tag_select2").select2({
				 multiple: true,
				 width: '100%',
				 	 maximumSelectionSize: 1,
       				formatSelectionTooBig: function (limit) {
            		return 'Chỉ Được Tạo 1 tag cho bài viết';
       				 },
				 
        		minimumInputLength: 8,
				 initSelection : function (element, callback) {
					var data = [];
					$(element.val().split(",")).each(function () {
						data.push({id: this, text: this});
					});
					callback(data);
				},
        			ajax: { 
        			url: "index.php?module=manage&act=blog&opt=ajax&feed_tag_video", 
        			dataType: 'json', 
					 quietMillis: 1000,
        					data: function (term, page){ 
        						return {q: term }; 
        						}, 
        						results: function (data, page) {
									console.log(data);
        							return { results: data }; 
									
        						}
        			}
			});
    } else {
        jQuery(".xcrud").find("#tag_select2").select2({
			  multiple: true,
				 width: '100%',
				 separator: ',',
				  maximumSelectionSize: 1,
       				formatSelectionTooBig: function (limit) {
            		return 'Chỉ Được Tạo 1 tag cho bài viết';
       				 },
        		minimumInputLength: 8,
        			ajax: { 
        			url: "index.php?module=manage&act=manga&opt=ajax&feed_tag_video", 
        			dataType: 'json', 
					 quietMillis: 1000,
        					data: function (term, page){ 
        						return {q: term }; 
        						}, 
        						results: function (data, page) {
									
        							return { results: data }; 
									
        						}
        			}
			
			});
    }
});
</script>

        <style>
    .glyphicon-ok.state01-1,.glyphicon-remove.state01-0{
    display:none;
	}
	.state01-0{ color: #0C6}
	.state01-1{ color: #F00}
	.glyphicon-ok.state10-0,.glyphicon-remove.state10-1{
    display:none;
	}
	.state10-1{ color:#0C6 }
	.state10-0{ color: #F00}
	</style>